<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/9
 * Time: 16:43
 */

namespace app\admin\controller;

use app\model\StationInfo as ModelStationInfo;
use app\logic\Excel as MobileLogicExcel;
use app\model\StationPassport as ModelStationPassport;
use app\model\WithdrawLog as ModelWithdrawLog;
use app\model\Express as ModelExpress;
use app\model\Area as ModelArea;
use app\model\ServerArea as ModelServerArea;

class Station extends Member
{
    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 站点信息列表导出
     * uri:/admin/station.listExport
     */
    public function stationListExport(){
        $arr_where="";
        $str="";
        $name = $this->_getContentParams('name');
        $level = $this->_getContentParams('level');
        $startTime = $this->_getContentParams('startTime');
        $endTime = $this->_getContentParams('endTime');
        $str_excel = $this->_getContentParams('excel');
        if(!empty($name)){
            $arr_where.=" station_info.name like '%$name%'";
            if (!empty($level) or !empty($startTime) or !empty($endTime)){
                $str=" and";
            }
        }

        if (!empty($level)){
            $arr_where.="$str station_info.level like '%$level%'";
            if (!empty($startTime) or !empty($endTime)){
                $str=" and";
            }
        }

        if (!empty($startTime)){
            $arr_where.="$str DATE_FORMAT(station_info.gmt_create,'%Y%m%d') >= DATE_FORMAT('$startTime','%Y%m%d')";
            if (!empty($endTime)){
                $str=" and";
            }
        }

        if (!empty($endTime)){
            $arr_where.="$str DATE_FORMAT(station_info.gmt_create,'%Y%m%d') <= DATE_FORMAT('$endTime','%Y%m%d')";
        }

        $list = ModelStationInfo::getInstance()->getStationInfoList($arr_where,'','');
        if($str_excel == 'excel'){
            MobileLogicExcel::getInstance()->export($list, ['id' => '站点ID', 'station_number' => '站点编号', 'name' => '站点名称', 'level' => '站点等级','gmtCreate'=>'注册时间','state'=>'账户启用状态(0：禁用；1：启用)'], '站点信息列表'.date('Y-m-d', time()));
        }

        _returnData();
    }

    /**
     * 站点账户状态修改
     * uri:/admin/station.state
     */
    public function stationState(){
        $stationId = $this->_getContentParams('stationId');
        _toValidate('id',$stationId,16001);
        $isEnable = $this->_getContentParams('isEnable');
        _toValidate('is_enable',$isEnable,10822);
        $arr_where=[
            'state'=>$isEnable
        ];
        $data = ModelStationInfo::getInstance()->toUpdate("id=$stationId",$arr_where);
        _returnData($data);
    }

    /**
     * 站点手机号/账号、身份证号判断是否存在
     * uri:/admin/station.judge
     */
    public function stationJudge(){
        $type = $this->_getContentParams('type');
        if ($type=='account'){
            $account = $this->_getContentParams('account');
            $data = ModelStationInfo::getInstance()->toFind("mobile=$account");
            if ($data>0){
                $judge=1;
            }else{
                $judge=0;
            }
            _returnData(['judge'=>$judge]);
        }elseif ($type=='idNumber'){
            $idNumber = $this->_getContentParams('idNumber');
            $data = ModelStationInfo::getInstance()->toFind("legal_idcard_no=$idNumber");
            if ($data>0){
                $judge=1;
            }else{
                $judge=0;
            }
            _returnData(['judge'=>$judge]);
        }
    }

    /**
     * 添加站点
     * uri:/admin/station.add
     */
    public function stationAdd(){
        $account = $this->_getContentParams('account');
        $loginAccount = $this->_getContentParams('loginAccount');
        $realName = $this->_getContentParams('realName');
        $idNumber = $this->_getContentParams('idNumber');
        $startTerm =$this->_getContentParams('startTerm');
        $endTerm = $this->_getContentParams('endTerm');
        $idcardFront = $this->_getContentParams('idcardFront');
        $idcardBack = $this->_getContentParams('idcardBack');
        $companyName = $this->_getContentParams('companyName');
        $companyNo = $this->_getContentParams('companyNo');
        $companyValidStartTime = $this->_getContentParams('companyValidStartTime');
        $companyValidEndTime = $this->_getContentParams('companyValidEndTime');
        $businessLicense = $this->_getContentParams('businessLicense');
        $openLicence = $this->_getContentParams('openLicence');
        $expressId = $this->_getContentParams('expressId');
        $branchName = $this->_getContentParams('branchName');
        $letterOfAuthority = $this->_getContentParams('letterOfAuthority');
        $state = $this->_getContentParams('state');
        $address = $this->_getContentParams('address');
        $lng = $this->_getContentParams('lng');
        $lat = $this->_getContentParams('lat');
        $emial = $this->_getContentParams('emial');
        $headPortrait = $this->_getContentParams('headPortrait');
        $str_pwd_key = $this->_getRandomString(8);
        $password = passwordEncryption('123456', $str_pwd_key);
        $area_id='';
        if (!empty($address)){
            $address_arr=json_decode($address,true);
            if (empty($address_arr['street_id'])){
                $area_id=$address_arr['county_id'];
            }else{
                $area_id=$address_arr['street_id'];
            }
            $area = ModelArea::getInstance()->toFind("id=$area_id");
            ModelServerArea::getInstance()->toAdd($area);
        }

        $arr_data=[
            'mobile'=>$account,
            'login_account'=>$loginAccount,
            'legal_name'=>$realName,
            'legal_idcard_no'=>$idNumber,
            'start_term'=>$startTerm,
            'end_term'=>$endTerm,
            'idcard_front'=>$idcardFront,
            'idcard_back'=>$idcardBack,
            'company_name'=>$companyName,
            'company_no'=>$companyNo,
            'company_valid_start_time'=>$companyValidStartTime,
            'company_valid_end_time'=>$companyValidEndTime,
            'business_license'=>$businessLicense,
            'open_licence'=>$openLicence,
            'express_id'=>$expressId,
            'branch_name'=>$branchName,
            'letter_of_authority'=>$letterOfAuthority,
            'state'=>$state,
            'address'=>$address,
            'lng'=>$lng,
            'lat'=>$lat,
            'pwd_key'=>$str_pwd_key,
            'password'=>$password,
            'area_id'=>$area_id,
            'head_portrait'=>$headPortrait,
            'emial'=>$emial
        ];
        //dump($arr_data);die;
        $data = ModelStationInfo::getInstance()->toAdd($arr_data);
        $stationNumber= getCharNumber($data,'ZD');
        ModelStationInfo::getInstance()->toUpdate("id=$data",['station_number'=>$stationNumber]);
        _returnData($data);
    }

    /**
     * 站点详情
     * uri:/admin/station.details
     */
    public function stationDetails(){
        $stationId = $this->_getContentParams('stationId');
        _toValidate('id',$stationId,16001);
        //基本信息
        $data = ModelStationInfo::getInstance()->toFind("id=$stationId","id,name,gmt_create AS gmtCreate,station_number AS stationNumber,legal_name AS legalName,address,express_id AS expressId,legal_idcard_no AS legalIdcardNo,start_term AS startTerm,end_term AS endTerm,idcard_front AS idcardFront,idcard_back AS idcardBack,company_no AS companyNo,company_name AS companyName,company_valid_start_time AS companyValidStartTime,company_valid_end_time AS companyValidEndTime,business_license AS businessLicense,open_licence AS openLicence,branch_name AS branchName,letter_of_authority AS letterOfAuthority");
        if ($data){
            $courierCompanyName = ModelExpress::getInstance()->getExpressName("express_id={$data['expressId']}");
            $data['courierCompanyName']=$courierCompanyName;
        }
        //登录次数
        $loginCnt = ModelStationPassport::getInstance()->toCount("station_id=$stationId");
        //提现总额
        $withdrawal = ModelWithdrawLog::getInstance()->toSum("map_id=$stationId and type=3",'money');
        if ($withdrawal==null){
            $withdrawal=0;
        }
        //入库总件数
        //$putInStorage = ModelStationInfo::getInstance()->toCount("");
        //dump($withdrawal);
        _returnData($data);
    }

    /**
     * 站点信息编辑
     * uri:/admin/station.editData
     */
    public function stationEditData(){
        $stationId = $this->_getContentParams('stationId');
        _toValidate('id',$stationId,16001);
        $arr_where['id'] = $stationId;
        $list = ModelStationInfo::getInstance()->toFind($arr_where,'id,mobile,legal_name AS legalName,legal_idcard_no AS legalIdcardNo,start_term AS startTerm,end_term AS endTerm,idcard_front AS idcardFront,idcard_back AS idcardBack,company_name AS companyName,company_no AS companyNo,company_valid_start_time AS companyValidStartTime,company_valid_end_time AS companyValidEndTime,business_license AS businessLicense,open_licence AS openLicence,express_id AS expressId,branch_name AS branchName,letter_of_authority AS letterOfAuthority,state,address');
        if ($list){
            $courierCompanyName = ModelExpress::getInstance()->getExpressName("express_id={$list['expressId']}");
            $list['courierCompanyName']=$courierCompanyName;
        }
        _returnData(['editData'=>$list]);
    }

    /**
     * 快递员资料编辑修改
     * uri:/admin/station.editDataUpdate
     */
    public function stationEditDataUpdate(){
        $stationId = $this->_getContentParams('stationId');
        _toValidate('id',$stationId,16001);
        $account = $this->_getContentParams('account');
        $realName = $this->_getContentParams('realName');
        $idNumber = $this->_getContentParams('idNumber');
        $startTerm =$this->_getContentParams('startTerm');
        $endTerm = $this->_getContentParams('endTerm');
        $idcardFront = $this->_getContentParams('idcardFront');
        $idcardBack = $this->_getContentParams('idcardBack');
        $companyName = $this->_getContentParams('companyName');
        $companyNo = $this->_getContentParams('companyNo');
        $companyValidStartTime = $this->_getContentParams('companyValidStartTime');
        $companyValidEndTime = $this->_getContentParams('companyValidEndTime');
        $businessLicense = $this->_getContentParams('businessLicense');
        $openLicence = $this->_getContentParams('openLicence');
        $expressId = $this->_getContentParams('expressId');
        $branchName = $this->_getContentParams('branchName');
        $letterOfAuthority = $this->_getContentParams('letterOfAuthority');
        $state = $this->_getContentParams('state');
        $address = $this->_getContentParams('address');
        $lng = $this->_getContentParams('lng');
        $lat = $this->_getContentParams('lat');
        $arr_data=[
            'mobile'=>$account,
            'legal_name'=>$realName,
            'legal_idcard_no'=>$idNumber,
            'start_term'=>$startTerm,
            'end_term'=>$endTerm,
            'idcard_front'=>$idcardFront,
            'idcard_back'=>$idcardBack,
            'company_name'=>$companyName,
            'company_no'=>$companyNo,
            'company_valid_start_time'=>$companyValidStartTime,
            'company_valid_end_time'=>$companyValidEndTime,
            'business_license'=>$businessLicense,
            'open_licence'=>$openLicence,
            'express_id'=>$expressId,
            'branch_name'=>$branchName,
            'letter_of_authority'=>$letterOfAuthority,
            'state'=>$state,
            'address'=>$address,
            'lng'=>$lng,
            'lat'=>$lat
        ];
        ModelStationInfo::getInstance()->toUpdate("id=$stationId",$arr_data);
        _returnData();
    }

    /**
     * 站点订单列表
     * uri:/admin/station.orderList
     */
    public function stationOrderList(){
        $stationId = $this->_getContentParams('stationId');
        _toValidate('id',$stationId,16001);

    }
}